﻿'
' Copyright (c) 2010-2012, MatthiWare
'
' Permission is hereby granted, free of charge, to any person obtaining a
' copy of this software and associated documentation files (the "Software"),
' to deal in the Software without restriction, including without limitation
' the rights to use, copy, modify, merge, publish, distribute, sublicense,
' and/or sell copies of the Software, and to permit persons to whom the
' Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in
' all copies or substantial portions of the Software. 
' You shall include 'MatthiWare' in the credits/about section of the program
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
' IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
' FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
' AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
' LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
' FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
' DEALINGS IN THE SOFTWARE.
'

Imports System
Imports System.IO
Imports System.Xml
Imports System.Xml.Serialization
Imports System.Threading
Imports SharpDX
Imports TerrariaVB.Core.Helper
Imports TerrariaVB.Core.Base
Imports SharpDX.Direct2D1

<Serializable()>
Public Class Item

    <XmlIgnore()>
    Public Shared ItemImageCollection As List(Of Bitmap)

    <XmlIgnore()>
    Public Const BaseItemName As String = "Item_"

    <XmlIgnore()>
    Public Const BasePath As String = "Items\"

    <XmlIgnore()>
    Public Const BaseImgExtension As String = ".png"

    <XmlIgnore()>
    Public Const BaseExtension As String = ".xml"

    <XmlIgnore()>
    Public Const BaseImage As String = "Img\"

    Property Name As String = "NULL"

    Property IsBlock As Boolean = False

    Property IsPlacable As Boolean = False

    Property IsPickaxe As Boolean = False

    Property IsAxe As Boolean = False

    Property IsMeleeWeapon As Boolean = False

    Property IsMagicWeapon As Boolean = False

    Property IsRangeWeapon As Boolean = False

    Property IsEquipAble As Boolean = False

    Property RangeDamage As Double = 0

    Property MagicDamage As Double = 0

    Property MeleeDamage As Double = 0

    Property IsConsumable As Boolean = False

    <XmlIgnore()>
    Private _t As Int32
    Public Property Type() As Int32
        Get
            Return _t
        End Get
        Set(ByVal value As Int32)
            _t = value
        End Set
    End Property

    Property IsStackable As Boolean = False

    Property Heals As Int32 = 0

    Property Defence As Double = 0

    Public Sub New(e As GameCoreEventArgs)
        Init(e)
    End Sub

    Public Sub Init(e As GameCoreEventArgs)
        Dim svcContent As Zip = e.Core.Services.GetService(GetType(Zip))
        Dim target As RenderTarget = e.Core.RenderTarget2D

        If (IsNothing(ItemImageCollection)) Then
            ItemImageCollection = New List(Of Bitmap)()
        End If

        If (Type >= ItemImageCollection.Count) Then
            ItemImageCollection.Add(BitmapHelper.LoadFromStream(target, svcContent.Extract(BasePath & BaseImage & BaseItemName & Type & BaseImgExtension), Drawing.Color.Fuchsia))
        Else
            ItemImageCollection(Type) = BitmapHelper.LoadFromStream(target, svcContent.Extract(BasePath & BaseImage & BaseItemName & Type & BaseImgExtension), Drawing.Color.Fuchsia)
        End If
    End Sub

    Public Shared Function GetImage(t As Int32) As Bitmap
        Return ItemImageCollection(t)
    End Function

    Public Shared Function LoadItem(t As Int32, e As GameCoreEventArgs) As Item
        Dim lockObj As New Object()

        Dim fs As FileStream = Nothing
        Dim itemPath As String = BasePath & t & BaseExtension
        Dim itemFileInf As FileInfo = New FileInfo(itemPath)
        Dim rtrnItem As Item = New Item(e)
        Dim classDecoder As XmlSerializer = Nothing

        Monitor.Enter(lockObj)
        Try
            If (itemFileInf.Exists) Then
                classDecoder = New XmlSerializer(rtrnItem.GetType())
                fs = itemFileInf.Open(FileMode.Open, FileAccess.Read, FileShare.Read)
                rtrnItem = CType(classDecoder.Deserialize(fs), Item)

            Else
                rtrnItem = New Item(e)
            End If
        Catch ex As Exception
            Throw ex
        Finally
            If (Not IsNothing(fs)) Then
                fs.Close()
                fs.Dispose()
                fs = Nothing
            End If
            Monitor.Exit(lockObj)
        End Try
        Return rtrnItem
    End Function

    Public Sub Save()
        Return

        Dim itemPath As String = BasePath & Type & BaseExtension
        Dim itemFI As FileStream = File.Open(itemPath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)
        Dim x As New XmlSerializer(Me.GetType())
        x.Serialize(itemFI, Me)
        itemFI.Dispose()
    End Sub

    Public Shared Sub CreateDefaults(e As GameCoreEventArgs)
        Dim i As New Item(e)
        i.Name = "NULL"
        i.Type = 0
        i.Save()
        i = New Item(e)
        i.Name = "Dirt"
        i.IsPlacable = True
        i.IsBlock = True
        i.IsStackable = True
        i.Type = 1
        i.Save()
        i = New Item(e)
        i.Name = "Stone"
        i.IsPlacable = True
        i.IsBlock = True
        i.IsStackable = True
        i.Type = 3
        i.Save()
        i = New Item(e)
        i.Name = "Stone Sword"
        i.IsMeleeWeapon = True
        i.MeleeDamage = 5
        i.Type = 4
        i.Save()
        i = New Item(e)
        i.Name = "Stone Pickaxe"
        i.IsStackable = True
        i.IsPickaxe = True
        i.IsMeleeWeapon = True
        i.MeleeDamage = 2
        i.Type = 5
        i.Save()
        i = New Item(e)
        i.Name = "Copper Ore"
        i.IsStackable = True
        i.Type = 6
        i.Save()
        i = New Item(e)
        i.Name = "Iron Ore"
        i.IsStackable = True
        i.Type = 7
        i.Save()
        i = New Item(e)
        i.Name = "Silver Ore"
        i.IsStackable = True
        i.Type = 8
        i.Save()
        i = New Item(e)
        i.Name = "Gold Ore"
        i.IsStackable = True
        i.Type = 9
        i.Save()
    End Sub

    Public Sub Render(e As DrawEventArgs, x As Double, y As Double)
        Dim img As Bitmap = ItemImageCollection(Type)
        Dim destRect As New DrawingRectangleF(x, y, img.Size.Width, img.Size.Height)
        e.Target.DrawBitmap(ItemImageCollection(Type), destRect, 1.0F, BitmapInterpolationMode.Linear)
    End Sub

End Class
